package com.woniuxy.oldmanapi.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.entity.OlderHeight;
import com.woniuxy.oldmanapi.service.HeightService;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import com.woniuxy.oldmanapi.vo.HeightVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class HeightController {

    @Autowired
    HeightService heightService;

    @GetMapping("/height/list")
    public ResponseResult<PageInfo<HeightVo>> list(
            @RequestParam(value = "now", defaultValue = "1") Integer now,
            @RequestParam(value = "size", defaultValue = "9") Integer size,
            String name) {
        ResponseResult<PageInfo<HeightVo>> result = new ResponseResult<>();
        try {
            PageInfo<HeightVo> lists = heightService.findHeightByPage(now, size, name);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(lists);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result;
    }

    @PostMapping("/height/add")
    public ResponseResult<Void> add(@RequestBody OlderHeight olderHeight) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            heightService.add(olderHeight);
            result.setCode(200);
            result.setMsg("添加成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("添加失败!");
        }
        return result;
    }

    @PutMapping("/height/modify")
    public ResponseResult<Void> modify(@RequestBody OlderHeight olderHeight) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            heightService.modify(olderHeight);
            result.setCode(200);
            result.setMsg("修改成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("修改失败!");
        }
        return result;
    }

    @DeleteMapping("/height/delete/{id}")
    public ResponseResult<Void> delete(@PathVariable("id")int id) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            heightService.delete(id);
            result.setCode(200);
            result.setMsg("删除成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("删除失败!");
        }
        return result;
    }

    @GetMapping("/height/one/{id}")
    public ResponseResult<HeightVo> one(@PathVariable("id") int id) {
        ResponseResult<HeightVo> result = new ResponseResult<>();
        try {
            HeightVo heightVo = heightService.selectOne(id);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(heightVo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result;
    }
}
